
package com.gugalor.adapter;

import android.content.Context;
import android.graphics.BitmapFactory.Options;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jianzhile.jobs.R;
import com.jianzhile.jobs.bean.BucketEntity;
import com.jianzhile.jobs.utils.CommonUtil;

import java.util.ArrayList;

/**
 * 选择相册 adapter
 */
public class BucketListAdapter extends BaseAdapter {

    private static final String TAG = "BucketListAdapter";
    private Context mContext;
    private ArrayList<BucketEntity> mBucketList;
    private LayoutInflater mInflater;
    private Options mDecodingOptions;

    // private DDApplication mApp;

    public BucketListAdapter(Context context, ArrayList<BucketEntity> bucketList) {
        super();
        this.mContext = context;
        this.mBucketList = bucketList;

        // mApp = (DDApplication) mContext.getApplicationContext();

        mInflater = LayoutInflater.from(mContext);

        mDecodingOptions = new Options();
        mDecodingOptions.outWidth = CommonUtil.getDisplayWidth(mContext) / 4;

    }

    @Override
    public int getCount() {
        return (null == mBucketList) ? 0 : mBucketList.size();
    }

    @Override
    public BucketEntity getItem(int position) {
        return (null == mBucketList) ? null : mBucketList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (null == mContext) {
            return null;
        }
        if (null == mBucketList || mBucketList.isEmpty() || mBucketList.size() <= position) {
            return null;
        }
        final ViewHolder holder;
        if (null == convertView) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.albums_list_item, parent, false);

            holder.previewImg = (ImageView) convertView.findViewById(R.id.preview_img);
            holder.nameTv = (TextView) convertView.findViewById(R.id.name_tv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        BucketEntity entity = mBucketList.get(position);

        String preview = entity.getPreviewPath();
        if (!TextUtils.isEmpty(preview)) {
        	Glide.with(mContext).load("file://" + preview).into(holder.previewImg);
        }

        holder.nameTv.setText(entity.getBucketName() + "");

        return convertView;
    }

    private class ViewHolder {
        // 相册缩略图
        ImageView previewImg;
        // 相册名称
        TextView nameTv;
    }

}
